/**
 * Created by sam on 16-12-12.
 */
var common = require('../conf/common');
var ckArr ={}; //


var agent = [
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:36.0) Gecko/20100101 Firefox/36.0",
    "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0)",
    "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
    "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36",
    "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
];
function random(min,max){
    return Math.floor(min+Math.random()*(max-min));

}

function getAgent(i) {
    return ckArr['ag_'+i]||agent[random(0,agent.length)];
}

function setAgent(i) {
    ckArr['ag_'+i] = agent[random(0,agent.length)];
}

var setCookies = function (i,cookies){
    var cookie = ckArr['ck_'+i] || {};
    for (var k in cookies){
        var sc = cookies[k].split(";")[0];
        var key = sc.split("=");
        if(key[1]!='-')
            cookie[key[0]] = sc.replace("\"","") ;
    }
    ckArr['ck_'+i] = cookie;
};
var getCookie = function getCookie(i) {
    var cookie = ckArr['ck_'+i]||{};
    var c = "";
    for (var k in cookie){
        c += cookie[k]+";";
    }
    return c;
};


var refresh = function refresh(i,fn) {
        var req = null;
        setAgent(i);
        ckArr['ck_'+i] = {};
        var url = common.getDomain()+"/gp/redirection/india.html";
        console.log('Thread['+i+'] refresh:'+url);
        var parse_u=require('url').parse(url,true);
        var isHttp=parse_u.protocol=='http:';
        var options={
            host:parse_u.hostname,
            port:parse_u.port||(isHttp?80:443),
            path:parse_u.path,
            method:'GET',
            headers:{
                'Accept':'*/*',
                'User-Agent':getAgent(i),
            }
        };

        req = require(isHttp?'http':'https').request(options,function(res){
            var cookies = res.headers['set-cookie'];
            res.on('error',function (e) {
                console.log(e);
            });
            setCookies(i,cookies);
            if(fn)fn(i);
        });
        req.on('error', function (e) {
            console.log('Thread['+i+'] req error'+e.code);
            if(fn)fn(i);
            //console.log(e);
        });
        req.end();

};
//module.exports.setCookie = setCookie;
module.exports.setCookies = setCookies;
module.exports.getCookie = getCookie;
module.exports.getAgent = getAgent;

module.exports.refresh = refresh;

